function GetRandomNum(Min, Max) {

    var Range = Max - Min;

    var Rand = Math.random();

    return (Min + Math.round(Rand * Range));

}

$(document).ready(function () {

    var tel = getQueryString("tel");
    var openId = getQueryString("openId");
    var appId = getQueryString("appId");
    $("#tip").hover(function () {
        $(this).attr("src", "resource/four2.png");
    }, function () {
        $(this).attr("src", "resource/four1.png");
    });

    var roll = function () {
        $(this).unbind('mouseenter').unbind('mouseleave');
        $(this).attr("src", "resource/four2.png");
        if (parseInt($(".coud_num").html()) <= 0) {
            sucessFun("<div class='strong'>谢谢参与!</div><div>今天的抽奖机会已经用完,明天再来吧～</div>");
            var json_error = "error_0";
            return false;
        }

        $(this).unbind("click", roll);
        /*
        * 说明 一等奖(1G)：1-2位 二等奖(700M):2-6 四位 三等奖(500M):6-14  8位 四等奖(150M)：14-89
        * 75位 五等奖(70M)：89-489  400位 六等奖(30M):489-2489 2000位 以后全部为不中奖 10000
        */
        var angle = 0;

        var maxrRandom = 10000;
        var one_start = 1;
        var one_stop = 2;
        var two_start = 2;
        var two_stop = 6;
        var three_start = 6;
        var three_stop = 14;
        var four_start = 14;
        var four_stop = 89;
        var five_start = 89;
        var five_stop = 489;
        var six_start = 489;
        var six_stop = 2489;
        var Rand_num = GetRandomNum(one_start, maxrRandom);

        if (Rand_num >=one_start && Rand_num <= one_stop) {
            var json_error = "win_1";
        } else if (Rand_num > two_start && Rand_num <= two_stop) {
            var json_error = "win_2";
        } else if (Rand_num > three_start && Rand_num <= three_stop) {
            var json_error = "win_3";
        } else if (Rand_num > four_start && Rand_num <= four_stop) {
            var json_error = "win_4";
        } else if (Rand_num > five_start && Rand_num <= five_stop) {
            var json_error = "win_5";
        } else if (Rand_num > six_start && Rand_num <= six_stop) {
            var json_error = "win_6";
        } else if (Rand_num > six_stop) {
            var json_error = "error_2";
        }

        switch (json_error) {
            case "error_0":
                sucessFun("<div class='strong'>谢谢参与!</div><div>今天的抽奖机会已经用完,明天再来吧～</div>");
                return false;
                break;
            case "win_1":
                var $dushu = 90 + 720;
                break;
            case "win_2":
                var $dushu = 180 + 720;
                break;
            case "win_3":
                var $dushu = 360 + 720;
                break;
            case "win_4":
                var $dushu = 225 + 720;
                break;
            case "win_5":
                var $dushu = 315 + 720;
                break;
            case "win_6":
                var $dushu = 270 + 720;
                break;
            case "error_2":

                var $dushu = 45 + 720;

                break;
            default:
        }

        var zhuan = setInterval(function () {
            var i = 10;

            angle += i;

            if (angle % 45 == 0) {
                $('#light').show();
            } else {
                $('#light').hide();
            }
            $("#img").rotate(angle);
            if (angle >= $dushu) {
                clearInterval(zhuan);
                if (!openId || !appId) {
                    sucessFun("<div class='strong'>请先关注内蒙古移动营业厅</div>");
                    return false;
                }
                switch (json_error) {

                    case "win_1":
                        $('#light').show();
                        // 现实中奖后具体逻辑
                        var str = "恭喜您,抽中1G流量"; //</div><div>好运滚滚来</div>
                        TakePrice(appId, openId, tel, str, "1");
                        break;
                    case "win_2":
                        var str = "恭喜您,抽中700M流量";
                        TakePrice(appId, openId, tel, str, "2");
                        break;
                    case "win_3":
                        var str = "恭喜您,抽中500M流量";
                        TakePrice(appId, openId, tel, str, "3");
                        break;
                    case "win_4":
                        //alert("恭喜您获得20!");
                        var str = "恭喜您,抽中150M流量";
                        TakePrice(appId, openId, tel, str, "4");
                        break;
                    case "win_5":
                        var str = "恭喜您,抽中70M流量";
                        TakePrice(appId, openId, tel, str, "5");
                        // alert("恭喜您获得10!");
                        break;
                    case "win_6":
                        //alert("恭喜您获得5!");
                        var str = "恭喜您,抽中30M流量";
                        TakePrice(appId, openId, tel, str, "6");
                        break;
                    case "error_2":
                        var str = "很遗憾,没中奖!下次加油";
                        TakePrice(appId, openId, tel, str, "0");
                        break;
                    default:
                }

                $(".coud_num").html($(".coud_num").html() - 1);
                $("#tip").bind("click", roll);

                $("#tip").attr("src", "resource/four1.png");
                $("#tip").hover(function () {
                    $(this).attr("src", "resource/four1.png");
                }, function () {
                    $(this).attr("src", "resource/four2.png");
                });

            }
        }, 50);

    }

    $("#tip").bind("click", roll);

    //获取参数
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
    function TakePrice(appId, opend, tels, prize, grade) {
        var data =
           {
               appId: appId,
               openId: opend,
               name: "wheel",
               tel: tels,
               result: prize,
               grade: grade
           };

        $.ajax({
            type: "POST",
            url: "/rest/game/end/wheel",
            data: data,
            success: function (result) {
                var va = eval('(' + result + ')');
                sucessFun("<div class='strong'>" + va.title + "</div><div>" + va.details + "</div>");
            }
        });
    }
    function sucessFun(content) {
        ymPrompt.succeedInfo(content, 220, 150, null, null);
    }
});
